using FireStarterModeller.BO;
using FireStarterModeller.UI;
using Habanero.Base;
using Habanero.BO;
using Habanero.Faces.Base;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI
{
    [TestFixture]
    public class TestSelectSolutionControl
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }


        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();

        }
        [Test]
        public void Test_CreateControl()
        {
            //--------------- Set up test pack ------------------

            //--------------- Test Preconditions ----------------

            //--------------- Execute Test ----------------------
            SelectSolutionControl selectSolutionControl = new SelectSolutionControl();
            //--------------- Test Result -----------------------
            Assert.AreEqual(1, selectSolutionControl.Controls.Count);
            Assert.IsInstanceOf(typeof(IPanel), selectSolutionControl.Controls[0]);
            IPanel panel = (IPanel)selectSolutionControl.Controls[0];
            Assert.AreEqual(3, panel.Controls.Count);
            Assert.IsInstanceOf(typeof(ILabel), panel.Controls[0]);
            Assert.IsInstanceOf(typeof(IBOComboBoxSelector), panel.Controls[1]);
            Assert.IsInstanceOf(typeof(IButtonGroupControl), panel.Controls[2]);
            IButtonGroupControl buttonGroupControl = (IButtonGroupControl)panel.Controls[2];
            Assert.AreEqual(1, buttonGroupControl.Controls.Count);
            Assert.IsNotNull(buttonGroupControl["Load"]);
        }

        private IButton GetLoadButton(SelectSolutionControl selectSolutionControl)
        {
            IPanel panel = (IPanel)selectSolutionControl.Controls[0];
            IButtonGroupControl buttonGroupControl = (IButtonGroupControl)panel.Controls[2];
            return buttonGroupControl["Load"];
        }
        [Test]
        public void Test_SolutionsLoaded()
        {
            //--------------- Set up test pack ------------------
            TestUtilsShared.CreateUnsavedValidDMSolution();
            TestUtilsShared.CreateUnsavedValidDMSolution();
            BusinessObjectCollection<DMSolution> solutions = new BusinessObjectCollection<DMSolution>();
            solutions.LoadAll("SolutionName");
            //--------------- Test Preconditions ----------------
            Assert.AreEqual(2, solutions.Count);
            //--------------- Execute Test ----------------------
            SelectSolutionControl selectSolutionControl = new SelectSolutionControl();
            //--------------- Test Result -----------------------
            Assert.IsNotNull(selectSolutionControl.Selector);
            Assert.AreEqual(solutions, selectSolutionControl.Selector.BusinessObjectCollection);
            Assert.AreEqual(3, selectSolutionControl.Selector.Items.Count);
        }

        [Test]
        public void Test_LoadClicked_ShouldRaiseEvent()
        {
            //--------------- Set up test pack ------------------
            DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
            BusinessObjectCollection<DMSolution> solutions = new BusinessObjectCollection<DMSolution>();
            solutions.LoadAll("SolutionName");
            SelectSolutionControl selectSolutionControl = new SelectSolutionControl();
            selectSolutionControl.Selector.SelectedBusinessObject = solution;
            DMSolution selectedSolution = null;
            bool eventFired = false;
            selectSolutionControl.SolutionSelected += delegate(object sender, BOEventArgs<DMSolution> e)
            {
                eventFired = true;
                selectedSolution = e.BusinessObject;
            };
            //--------------- Test Preconditions ----------------
            Assert.AreEqual(1, solutions.Count);
            Assert.AreSame(solution, selectSolutionControl.Selector.SelectedBusinessObject);
            Assert.IsFalse(eventFired);
            Assert.IsNull(selectedSolution);
            //--------------- Execute Test ----------------------
            GetLoadButton(selectSolutionControl).PerformClick();
            //--------------- Test Result -----------------------
            Assert.IsTrue(eventFired);
            Assert.AreSame(solution, selectedSolution);
        }

    }

}